//
//  DHWelcomeController.swift
//  Weibo
//
//  Created by Holfioo on 15/9/5.
//  Copyright © 2015年 Holfioo. All rights reserved.
//

import UIKit
import SDWebImage

class DHWelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        iconView.sd_setImageWithURL(DHUserAccountViewModel.sharedUserAccount.iconUrl)
    }
    // 头像的约束
    private var iconViewCons: NSLayoutConstraint?
    // 在界面出现的时候。进行动画的进行。改变约束
    override func viewDidAppear(animated: Bool) {
        // 计算图标约束的最终位置。
        //  print(view.bounds.height)
        let offsetY = -(UIScreen.mainScreen().bounds.height + iconViewCons!.constant)
        // 将值返回去。
        iconViewCons?.constant = offsetY
        // 开始的时候文字不出现
        lab.alpha = 0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            // 执行布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.lab.alpha = 1
                    }, completion: { (_) -> Void in
                        // TODO: 做跳转
                    NSNotificationCenter.defaultCenter().postNotificationName(DHSwitchRootViewController, object: nil)
                })
        }
    }
    
    private func setupUI() {
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(lab)
        
        // 设置约束
//        backImageView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v" : backImageView]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-|", options: [], metrics: nil, views: ["v" : backImageView]))
        backImageView.ff_Fill(view)
        
//        iconView.translatesAutoresizingMaskIntoConstraints  = false
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -200))
//        iconViewCons = view.constraints.last
//        // 设置头像的宽高
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        let con = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -200))
        self.iconViewCons = iconView.ff_Constraint(con, attribute: NSLayoutAttribute.Bottom)
        
//        lab.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: lab, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: lab, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        lab.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
    }
    
    //MARK: - 懒加载控件
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
        let image = UIImageView(image: UIImage(named: "avatar_default_big"))
        image.layer.cornerRadius = 45
        image.layer.masksToBounds = true
        return image
    }()
    private lazy var lab: UILabel = UILabel(title: "欢迎归来", color: UIColor.darkGrayColor(), fontSize: 20)
}
